package tech.yangzhijie.basic.thread;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 用于测试线程
 */
public class ThreadTest {

    public static void main(String[] args) {
        //method01();
        //method02();
        //method03();
        //method04();
        //method05();
        //method06();
        //method07();
        //method08();
        method09();
    }

    //656练习1
    private static void method01(){
        for(int i=0;i<5;i++){
            new Thread(new PracticeRunnable(),"线程"+(i+1)).start();
        }
        System.out.println("等待 PracticeRunnable");
    }

    //656练习2
    private static void method02(){
        for(int i=1;i<=5;i++){
            new Thread(new FibonacciRunnable(i*2),"线程"+(i)).start();
        }
        System.out.println("等待 PracticeRunnable");
    }

    private static void method03(){
        ExecutorService service = Executors.newCachedThreadPool();
        for(int i=0;i<5;i++)
            service.execute(new LiftOff());
        service.shutdown();
    }

    private static void method04(){
        ExecutorService service = Executors.newSingleThreadExecutor();
        for(int i=0;i<5;i++)
            service.execute(new LiftOff());
        service.shutdown();
    }

    //658练习3
    private static void method05(){
        /*ExecutorService executorService = Executors.newCachedThreadPool();
        for(int i=1;i<=5;i++){
            executorService.execute(new PracticeRunnable());
        }
        executorService.shutdown();*/

        ExecutorService executorService1 = Executors.newSingleThreadExecutor();
        for(int i=1;i<=5;i++){
            executorService1.execute(new PracticeRunnable());
        }
        executorService1.shutdown();
    }

    //658练习4
    private static void method06(){

        ExecutorService executorService = Executors.newCachedThreadPool();
        for(int i=1;i<=5;i++){
            executorService.execute(new FibonacciRunnable(10));
        }
        executorService.shutdown();

        /*ExecutorService service = Executors.newSingleThreadExecutor();
        for(int i=1;i<=5;i++)
            service.execute(new FibonacciRunnable(7));
        service.shutdown();*/
    }

    private static void method07(){
        List<Future<String>> list = new ArrayList<>();
        ExecutorService executorService = Executors.newCachedThreadPool();
        for(int i=0;i<10;i++){
            list.add(executorService.submit(new ResultCallable(i)));
        }

        list.forEach(e->{
            try {
                System.out.println(e.get());
            }catch (Exception e1){
                e1.printStackTrace();
            }finally {
                executorService.shutdown();
            }
        });

    }

    private static void method08(){
        List<Future<Integer>> list = new ArrayList<>();
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++) {
            list.add(executorService.submit(new FibonacciCallable(i*2)));
        }
        for (Future<Integer> integerFuture : list) {
            try{
                System.out.println(integerFuture.get());
            }catch (Exception e1){
                e1.printStackTrace();
            }finally {
                executorService.shutdown();
            }
        }

    }

    private static void method09(){
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++) {
            executorService.execute(new SleepTaskRunnable());
        }
        executorService.shutdown();
    }
}
